package com.muyu.common.core.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * 订单状态枚举
 */
@Getter
@AllArgsConstructor
public enum OrderEnum {
     WXPAY(1,"微信"),

    balance(2,"余额"),

    alipay(3,"支付宝"),
    /**
     * 待支付
     */
    UNPAID(1, "待支付"),

    /**
     * 已支付
     */
    PAID(2, "已支付"),
    /**
     * 已成功
     */
     HAVE(3, "已成功"),

    /**
     * 支付失败
     */
    PAY_FAIL(4, "支付失败"),

    /**
     * 已关闭
     */
    CLOSED(8, "已关闭"),

    /**
     * 已退款
     */
    REFUNDED(9, "已退款"),
    /**
     * 退款中
     */
    REFUNDING(5, "退款中"),
    /**
     * 退款成功
     */
    REFUND(5, "退款成功"),
    /**
     * 退款失败
     */
    fail_FAIL(7, "退款失败");






    /**
     * 状态码
     */
    private final Integer code;

    /**
     * 状态描述
     */
    private final String desc;

    /**
     * 根据状态码获取枚举
     */
    public static OrderEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        return Arrays.stream(values())
                .filter(status -> Objects.equals(status.code, code))
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据状态码获取描述
     */
    public static String getDesc(Integer code) {
        OrderEnum status = getByCode(code);
        return status != null ? status.getDesc() : "";
    }

    /**
     * 判断是否为待支付状态
     */
    public static boolean isUnpaid(Integer status) {
        return UNPAID.getCode().equals(status);
    }

    /**
     * 判断是否为已支付状态
     */
    public static boolean isPaid(Integer status) {
        return PAID.getCode().equals(status);
    }

    /**
     * 判断是否为已关闭状态
     */
    public static boolean isClosed(Integer status) {
        return CLOSED.getCode().equals(status);
    }

    /**
     * 判断是否为可支付状态
     */
    public static boolean isPayable(Integer status) {
        return UNPAID.getCode().equals(status) ||
                PAID.getCode().equals(status) ||
                PAY_FAIL.getCode().equals(status);
    }
}
